package com.dwusoft.sendmsg.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/** 
 * @{#} MD5.java Create on Sep 5, 2008 4:54:43 PM 
 * <p>
 *  MD5 加密.
 * </p>
 * @author fguo
 * @version v 0.1
 */
public class MD5Utils {
	
	public static final int OXFF = 0xff;
	public static final int OX10 = 0x10;
	
	/**
	 * 
	 */
	private MD5Utils() {
		
	}
	
	/**
	 * 加密
	 * @param str
	 * @param type 0  大写 1 小写
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
    public static String crypt(String str,int type) throws NoSuchAlgorithmException {
        if (str == null || str.length() == 0) {
            throw new IllegalArgumentException("String to encript cannot be null or zero length");
        }

        StringBuffer hexString = new StringBuffer();

        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] hash = md.digest();

        for (byte aHash : hash) {
            if ((OXFF & aHash) < OX10) {
                hexString.append("0").append(Integer.toHexString((OXFF & aHash)));
            } else {
                hexString.append(Integer.toHexString(OXFF & aHash));
            }
        }
        if(type==0){
        	return hexString.toString().toUpperCase();
        }else{
        	return hexString.toString();
        }
    }
    
    public static void main(String[] args) throws NoSuchAlgorithmException {
    	System.out.println("B5A9241D166BA1913AD4241C3584DD3F");
    	System.out.println(crypt("SDK-WSS-010-10097012-10=4-44",0));
//    	System.out.println(crypt("1").equals("c4ca4238a0b923820dcc509a6f75849b"));
	}
}
